﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data.Sql;
using System.Collections.Concurrent;
using System.Data.Odbc;

namespace QuanLyQuangCao
{
    public partial class frmDangKy_Edit : Form
    {
        public static frmDangKy_Edit Edit;
        
        SqlConnection Conn = new SqlConnection(C_Conn.Connection);
        public SqlDataAdapter da ;//= new SqlDataAdapter();
        public DataSet ds = new DataSet();
        public DataTable dt = new DataTable();
        public BindingSource bds = new BindingSource();
        
        public frmDangKy_Edit()
        {
            Edit = this;
            InitializeComponent();
        }      
        private void frmNewDangKy_Load(object sender, EventArgs e)
        {
            string sql = @"SELECT * FROM KhachHang";
            da = new SqlDataAdapter(sql, Conn);
            da.Fill(ds, "KhachHang");


            cboMa_Khach.DataSource = ds.Tables["KhachHang"];
            cboMa_Khach.DisplayMember = "Ten_Khach";
            cboMa_Khach.ValueMember = "Ma_Khach";

            dgvDangKy_Edit.AllowUserToAddRows = true;            
            FillData();           
        }

        private void btAccept_Click(object sender, EventArgs e)
        {
            dgvDangKy_Edit.AllowUserToAddRows = false;
            Conn.Open();

            if (frmDangKy_View.View.bButton == "Them")
            {
                //foreach (DataRow drMa_Khach in dt.Rows)
                //{

                //}
                

                if (dgvDangKy_Edit.Rows.Count > 0)
                {
                    for (int i = 0; i < dgvDangKy_Edit.Rows.Count; i++)
                    {
                        string sqlInsert = @"INSERT INTO DangKyQuangCao(Ma_Khach, Ma_Loai, Gia_mot_Ky, So_Ky_Dang, Ngay_Bat_Dau, Ma_Nhan_Vien) " +
                                    "VALUES (@Ma_Khach, @Ma_Loai, @Gia_Mot_Ky, @So_Ky_Dang, @Ngay_Bat_Dau, @Ma_Nhan_Vien)";
                        
                        SqlCommand cmdInsert = new SqlCommand(sqlInsert, Conn);

                        cmdInsert.Parameters.Add("@Ma_Khach", SqlDbType.NVarChar, 10);
                        cmdInsert.Parameters.Add("@Ma_Loai", SqlDbType.NVarChar, 5);
                        cmdInsert.Parameters.Add("@Gia_Mot_Ky", SqlDbType.Int);
                        cmdInsert.Parameters.Add("@So_Ky_Dang", SqlDbType.Int);
                        cmdInsert.Parameters.Add("@Ngay_Bat_Dau", SqlDbType.Date);
                        cmdInsert.Parameters.Add("@Ma_Nhan_Vien", SqlDbType.NVarChar, 10);

                        cmdInsert.Parameters["@Ma_Khach"].Value = dgvDangKy_Edit[1, i].Value.ToString();
                        
                        cmdInsert.Parameters["@Ma_Loai"].Value = dgvDangKy_Edit[2, i].Value.ToString();
                        cmdInsert.Parameters["@Gia_Mot_Ky"].Value = Convert.ToInt16(dgvDangKy_Edit[3, i].Value);
                        cmdInsert.Parameters["@So_Ky_Dang"].Value = Convert.ToInt16(dgvDangKy_Edit[4, i].Value);
                        cmdInsert.Parameters["@Ngay_Bat_Dau"].Value = dgvDangKy_Edit[5, i].Value;
                        cmdInsert.Parameters["@Ma_Nhan_Vien"].Value = dgvDangKy_Edit[6, i].Value.ToString();

                        cmdInsert.ExecuteNonQuery();
                        
                    }
                }

                MessageBox.Show("Đã thực hiện xong", "Thông báo", MessageBoxButtons.OK);
                this.Close();
                CapNhat();
            }

            if (frmDangKy_View.View.bButton == "Sua")
            {                
                //Xoa dong du lieu cu
                string sqlXoa = @"DELETE FROM DangKyQuangCao WHERE Ma_Dang_Ky = @Ma_Dang_Ky";
                SqlCommand cmdXoa = new SqlCommand(sqlXoa, Conn);
                cmdXoa.Parameters.Add("@Ma_Dang_Ky", SqlDbType.Int);
                cmdXoa.Parameters["@Ma_Dang_Ky"].Value = dgvDangKy_Edit.CurrentRow.Cells[0].Value;
                //cmdXoa.Parameters["@Ma_Dang_Ky"].Value = dgvDangKy_Edit.CurrentRow.Cells["Ma_Dang_Ky"].Value;
                cmdXoa.ExecuteNonQuery();

                //Insert dong du lieu moi
                string sqlInsert = @"INSERT INTO DangKyQuangCao(Ma_Khach, Ma_Loai, Gia_mot_Ky, So_Ky_Dang, Ngay_Bat_Dau, Ma_Nhan_Vien) " +
                                    "VALUES (@Ma_Khach, @Ma_Loai, @Gia_Mot_Ky, @So_Ky_Dang, @Ngay_Bat_Dau, @Ma_Nhan_Vien)";
                SqlCommand cmdInsert = new SqlCommand(sqlInsert, Conn);

                cmdInsert.Parameters.Add("@Ma_Khach", SqlDbType.NVarChar, 10);
                cmdInsert.Parameters.Add("@Ma_Loai", SqlDbType.NVarChar, 5);
                cmdInsert.Parameters.Add("@Gia_Mot_Ky", SqlDbType.Int);
                cmdInsert.Parameters.Add("@So_Ky_Dang", SqlDbType.Int);
                cmdInsert.Parameters.Add("@Ngay_Bat_Dau", SqlDbType.Date);
                cmdInsert.Parameters.Add("@Ma_Nhan_Vien", SqlDbType.NVarChar, 10);

                //cmdInsert.Parameters["@Ma_Khach"].Value = dgvDangKy_Edit.CurrentRow.Cells["Ma_Khach"].Value.ToString();
                //cmdInsert.Parameters["@Ma_Loai"].Value = dgvDangKy_Edit.CurrentRow.Cells["Ma_Loai"].Value.ToString();
                //cmdInsert.Parameters["@Gia_Mot_Ky"].Value = Convert.ToInt16(dgvDangKy_Edit.CurrentRow.Cells["Gia_Mot_Ky"].Value);
                //cmdInsert.Parameters["@So_Ky_Dang"].Value = Convert.ToInt16(dgvDangKy_Edit.CurrentRow.Cells["So_Ky_Dang"].Value);
                //cmdInsert.Parameters["@Ngay_Bat_Dau"].Value = dgvDangKy_Edit.CurrentRow.Cells["Ngay_Bat_Dau"].Value;
                //cmdInsert.Parameters["@Ma_Nhan_Vien"].Value = dgvDangKy_Edit.CurrentRow.Cells["Ma_Nhan_Vien"].Value.ToString();

                cmdInsert.Parameters["@Ma_Khach"].Value = dgvDangKy_Edit.CurrentRow.Cells[1].Value.ToString();
                cmdInsert.Parameters["@Ma_Loai"].Value = dgvDangKy_Edit.CurrentRow.Cells[2].Value.ToString();
                cmdInsert.Parameters["@Gia_Mot_Ky"].Value = Convert.ToInt16(dgvDangKy_Edit.CurrentRow.Cells[3].Value);
                cmdInsert.Parameters["@So_Ky_Dang"].Value = Convert.ToInt16(dgvDangKy_Edit.CurrentRow.Cells[4].Value);
                cmdInsert.Parameters["@Ngay_Bat_Dau"].Value = dgvDangKy_Edit.CurrentRow.Cells[5].Value;
                cmdInsert.Parameters["@Ma_Nhan_Vien"].Value = dgvDangKy_Edit.CurrentRow.Cells[6].Value.ToString();

                cmdInsert.ExecuteNonQuery();
                Conn.Close();

                MessageBox.Show("Đã thực hiện xong", "Thông báo", MessageBoxButtons.OK);
                this.Close();
                CapNhat();
            }
        }
        
        private void FillData()
        {
            if (frmDangKy_View.View.bButton == "Them")
            {
                
                string strSQL = @"SELECT Ma_Dang_Ky, Ma_Khach, Ma_Loai, Gia_Mot_Ky, So_Ky_Dang, Ngay_Bat_Dau, Ma_Nhan_Vien FROM DangKyQuangCao WHERE 0=1";

                SqlDataAdapter da = new SqlDataAdapter();
                da.SelectCommand = new SqlCommand(strSQL, Conn);
                DataSet ds = new DataSet();
                da.Fill(ds, "DangKyQuangCao");
                DataTable dt = new DataTable();
                dt = ds.Tables["DangKyQuangCao"];
                //Sua lai ten cot
                frmDangKy_View.View.GetColumn(dt);
                BindingSource bds = new BindingSource();
                bds.DataSource = dt;
                dgvDangKy_Edit.DataSource = bds;
                //An cot Ma_Dang_Ky
                dgvDangKy_Edit.Columns[0].Visible = false;               
                //dgvDangKy_Edit.Columns["Ma_Dang_Ky"].Visible = false;               
                
            }

            if (frmDangKy_View.View.bButton == "Sua")
            {
                dgvDangKy_Edit.DataSource = frmDangKy_View.View.bdsGridView();
                //An cot Ma_Dang_Ky
                dgvDangKy_Edit.Columns[0].Visible = false;
                //dgvDangKy_Edit.Columns["Ma_Dang_Ky"].Visible = false;               
                
            }           
        }

        private void btCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void CapNhat()  
        {
            //Cap nhat lai du lieu cho frmDangKy_View
            string chuoi = @"SELECT Ma_Dang_Ky, Ma_Khach, Ma_loai, Gia_Mot_Ky, So_Ky_Dang, Ngay_Bat_Dau, Ma_Nhan_Vien FROM DangKyQuangCao";
            da = new SqlDataAdapter(chuoi, Conn);
            da.Fill(ds, "DangKyQuangCao");
            dt = ds.Tables["DangKyQuangCao"];

            frmDangKy_View.View.GetColumn(dt);
            frmDangKy_View.View.bds.DataSource = dt;
            frmDangKy_View.View.Update(frmDangKy_View.View.bds);
        }        
    }
}
